// Y轴设置
import React, { useState } from 'react';
import { Switch, InputNumber, Input } from 'antd';
import Editor from 'src/pages/AnalyseManage/EditReport/components/EditorStyleType/Editor';
import FormItem from 'antd/lib/form/FormItem';

interface IProps {
	config: any;
	onValuesChange: ( chartType: string, key: string, values: Object, configType: string ) => void;
	form: any;
}

const DXAxis = ( props: IProps ) => {
	const { config, onValuesChange, form } = props;

	const fontConfig = [ 'fontFamily', 'fontSize', 'fontColor', 'fontBold', 'fontItalic' ];

	const [ isShowDXLine, setIsShowDXLine ] = useState(
		config.style && config.style.hasOwnProperty( 'isShowDXLine' ) ? config.style.isShowDXLine : false
	);
	const [ isShowDXLineName, setIsShowDXLineName ] = useState(
		config.style && config.style.hasOwnProperty( 'isShowDXLineName' ) ? config.style.isShowDXLineName : false
	);

	const onChange = ( checked: boolean, name: string, cb: any ) => {
		// setIsShowXLineName( checked );
		cb && cb();
		form.setFieldsValue( { [name]: checked } );
		onValuesChange( config.type, config.key, { [name]: checked }, 'style' );
	};

	const onBlur = ( value: any, name: string ) => {
		onValuesChange( config.type, config.key, { [name]: form.getFieldsValue()[name] }, 'style' );
	};

	return (
		<div>
			<FormItem
				label="是否显示轴线"
				name="isShowDXLine"
				labelAlign="left"
				labelCol={{ span: 8 }}
				wrapperCol={{ span: 16 }}
			>
				<Switch
					checked={isShowDXLine}
					onChange={( checked ) => onChange( checked, 'isShowDXLine', setIsShowDXLine( checked ) )}
				></Switch>
			</FormItem>
			{isShowDXLine && (
				<>
					<FormItem
						label="是否显示轴名称"
						name="isShowDXLineName"
						labelAlign="left"
						labelCol={{ span: 8 }}
						wrapperCol={{ span: 16 }}
					>
						<Switch
							checked={isShowDXLineName}
							onChange={( checked ) => onChange( checked, 'isShowDXLineName', setIsShowDXLineName( checked ) )}
						></Switch>
					</FormItem>

					{isShowDXLineName && (
						<>
							<FormItem label="X轴名称" name="chartDXAxisName">
								<Input
									onBlur={( value ) => onBlur( value, 'chartDXAxisName' )}
									onPressEnter={( value ) => onBlur( value, 'chartDXAxisName' )}
								></Input>
							</FormItem>
							<FormItem
								label="字体"
								name="chartDXLineFont"
								labelAlign="left"
								labelCol={{ span: 8 }}
								wrapperCol={{ span: 16 }}
							>
								<Editor
									config={config}
									form={form}
									onValuesChange={onValuesChange}
									fontFamilyName="DXLineFontFamily"
									fontSizeName="DXLineFontSize"
									fontColorName="DXLineFontColor"
									boldName="DXLineFontBold"
									italicName="DXLineFontItalic"
									fontConfig={fontConfig}
								/>
							</FormItem>
						</>
					)}

					<FormItem
						label="轴标签倾斜度(-90至90)"
						name="chartDXAxisLabelTilt"
						labelAlign="left"
						labelCol={{ span: 10 }}
						wrapperCol={{ span: 14 }}
					>
						<InputNumber min={-90} max={90}></InputNumber>
					</FormItem>
				</>
			)}
		</div>
	);
};

export default React.memo( DXAxis );
